package com.ittx.dzt.baiwang.service;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baiwang.open.client.BWRestClient;
import com.baiwang.open.client.IBWClient;
import com.baiwang.open.entity.request.SOutputinvoiceInvoiceRequest;
import com.baiwang.open.entity.request.SOutputinvoiceQueryPageRequest;
import com.baiwang.open.entity.response.SOutputinvoiceInvoiceResponse;
import com.baiwang.open.entity.response.SOutputinvoiceQueryPageResponse;
import com.ittx.dzt.baiwang.factory.BaiWangSdkClientFactory;
import com.ittx.dzt.baiwang.handler.AbstractBaseBaiWangApiHandler;
import com.ittx.dzt.edi.constant.PlatformMessageConstant;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.StoreConfigDTO;
import com.ittx.dzt.edi.vo.ResponseMessageVO;
import com.ittx.dzt.log.monitor.ExceptionLog;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 查询发票
 * <p>
 *
 * </p>
 *
 * @author wl_code@163.com
 * @version 1.0
 * @date 2025/6/18
 */
@Slf4j
@Service
public class QueryInvoiceService extends AbstractBaseBaiWangApiHandler<String> {

	@Override
	public String getLocalApiMethod() {
		return PlatformApiConstant.InvoiceInterface.INVOICE_QUERY;
	}

	@Override
	public Result<?> doProcess(PlatformApiParamDTO platformApiParamDTO, String reqJson) {
		try {
			Map<String, Object> extendMap = platformApiParamDTO.getExtendMap();
			StoreConfigDTO storeConfig = platformApiParamDTO.getStoreConfig();

			SOutputinvoiceInvoiceRequest request = JacksonTool.toEntity(reqJson, SOutputinvoiceInvoiceRequest.class);
			// IBWClient client = new BWRestClient(storeConfig.getSdkUrl(), MapUtil.getStr(extendMap, "bwAppKey"), MapUtil.getStr(extendMap, "bwAppSecret")); // 初始化一个客户端
			IBWClient client = BaiWangSdkClientFactory.getSdkClient(storeConfig.getSdkUrl(), MapUtil.getStr(extendMap, "bwAppKey"), MapUtil.getStr(extendMap, "bwAppSecret"));
			SOutputinvoiceInvoiceResponse response = client.sOutputinvoice().invoice(request, storeConfig.getSessionKey(), platformApiParamDTO.getRequestId());
			if (!response.getSuccess()) {
				String logStr = StrUtil.format("platform-response:{}", JacksonTool.toJson(response));
				log.error(PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), platformApiParamDTO.getApi(), logStr);
				return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0003,
						StrUtil.format(PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), platformApiParamDTO.getApi(), logStr)));
			}
			return Result.success().addData(response.getResponse());
		} catch (Exception e) {
			String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), this.getBaiWangMethod(), e.getMessage());
			ExceptionLog.error(e, PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), this.getBaiWangMethod(), e.getMessage());
			return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0003, msg)).addMsg(msg);
		}
	}

	@Override
	public String getBaiWangMethod() {
		return "baiwang.s.outputinvoice.queryPage";
	}
}
